package com.zzb.framework.frameworkcontroller;

import com.alibaba.fastjson.JSON;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.frameworkcontroller.common.FrameworkBaseController;
import com.zzb.framework.role.vo.UserLoginVo;
import com.zzb.framework.user.entity.User;
import com.zzb.framework.user.form.UserLoginForm;
import com.zzb.framework.user.service.LoginHistoryService;
import com.zzb.framework.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangzhenbin on 17-5-2.
 */
@RequestMapping("/loginframework")
@Controller

public class LoginFrameworkController extends FrameworkBaseController {
    private static final Logger logger = LoggerFactory.getLogger(LoginHistoryService.class);
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(){
        logger.debug("user login page");
        return "framework/login";
    }

    @RequestMapping("/loginPost")
    @ResponseBody
    public BaseResponse<UserLoginVo> loginPost(UserLoginForm userLoginForm, HttpServletRequest request){
        List<User> users = userService.selectAll();
        logger.info(JSON.toJSONString(users));
        BaseResponse<UserLoginVo> loginRet = userService.login(userLoginForm,request.getLocalAddr());
        if(loginRet.isSuccess()){
            saveUserSession(request,loginRet.getContent());
        }
        return loginRet;
    }
    @RequestMapping("/logout")
    @ResponseBody
    public BaseResponse logout(HttpServletRequest request){
        removeUserSession(request);
        return new BaseResponse(BaseResponse.SUCCESS,"登出成功");
    }
}
